package com.bawei.module_home.good_details;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.library_base.RouterPath;
import com.bawei.library_base.mvp.v.BaseActivity;
import com.bawei.library_base.room.good.Good;
import com.bawei.library_base.room.good.GoodDatabase;
import com.bawei.library_base.utils.GlideUtil;
import com.bawei.module_home.R;
import com.bawei.module_home.mvp.Goods;
import com.blankj.utilcode.util.ToastUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

@Route(path = RouterPath.DetailsGoods.DETAILS_GOODS)
public class Good_details extends BaseActivity {


    private Banner cdBanner;
    private TextView cdTv;
    private ImageView iv1;
    private ImageView iv2;
    private Button btAddShopping;
    private Button btPurchase;
    private Goods.DataBean dataBeans;
    private ImageView mibCollect;

    @Override
    public int beanLayout() {
        return R.layout.activity_good_details;
    }

    @Override
    public void initView() {
        dataBeans = (Goods.DataBean) getIntent().getSerializableExtra("dataBeans");



        cdBanner = (Banner) findViewById(R.id.cd_banner);
        cdTv = (TextView) findViewById(R.id.cd_tv);
        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        btAddShopping = (Button) findViewById(R.id.bt_addShopping);
        btPurchase = (Button) findViewById(R.id.bt_purchase);
        mibCollect = (ImageView) findViewById(R.id.mib_collect);
    }

    @Override
    public void initData() {

        //详情轮播
        banner();
        //商品详情
        goodsname();
        //下面俩详情图
        towimg();
        //加入购物车
        addshopping();
        //立即购买
        purchase();

        //商品详情进购物车
        goshopp();

    }

    private void goshopp() {
        mibCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                EventBus.getDefault().post("111");
                ARouter.getInstance().build(RouterPath.App.PAGER_HOME)
                        .navigation();
            }
        });
    }

    private void purchase() {
        btPurchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(RouterPath.SearchGoods.SEARCH_GOODS)

                        .navigation();
                ToastUtils.showShort("此功能不成熟");
            }
        });

    }

    private void addshopping() {
        btAddShopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Good good = new Good();
                good.setCheck(false);
                good.setCount(1);
                good.setImgUrl(dataBeans.getPictUrl());
                good.setPrice(dataBeans.getZkFinalPrice());
                good.setName(dataBeans.getCategoryName());
                good.setGood_id(dataBeans.getItemId());

                GoodDatabase.getInstance(getApplication()).getGoodDao().insert(good);
                ToastUtils.showShort("加入购物车");
            }
        });
    }

    private void towimg() {
        if (dataBeans.getSmallImages()==null){
            ToastUtils.showShort("无图");
        }else {
            GlideUtil.getInstance().LoadImage(this,dataBeans.getSmallImages().get(0),iv2);
            GlideUtil.getInstance().LoadImage(this,dataBeans.getSmallImages().get(1),iv1);
        }

    }

    private void goodsname() {
        cdTv.setText(dataBeans.getShortTitle());
    }

    private void banner() {
        List<String> smallImages = dataBeans.getSmallImages();

        if (smallImages==null){
            ToastUtils.showShort("无图");
        }else {
            cdBanner.setImages(smallImages);
            cdBanner.isAutoPlay(false);
            cdBanner.setBannerStyle(BannerConfig.NUM_INDICATOR);
            cdBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    GlideUtil.getInstance().LoadImage(context,path,imageView);
                }
            }).start();
        }

    }
}